package com.sjlh.centurylianhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sjlh.centurylianhua.dto.ModuleDto;
import com.sjlh.centurylianhua.dto.ModulePath;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.Module;
import com.sjlh.centurylianhua.mapper.ModuleMapper;
import com.sjlh.centurylianhua.utils.CommonUtil;
import com.sjlh.centurylianhua.utils.ResultData;
import com.sjlh.centurylianhua.utils.UITreeFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/module")
@CrossOrigin
public class ModuleController {
    @Resource
    private ModuleMapper moduleMapper;
    /**
     * 分页查询
     *
     * */
    @RequestMapping("/getAll")
    public Object getAll(@RequestBody SearchCon sc){
        QueryWrapper<Module> wrapper = new QueryWrapper<>();
        //wrapper.orderByAsc("weight");
        List<ModuleDto> mdtoList=new ArrayList<ModuleDto>();//所有的已转的模块业务对象
        if(sc.getTypeName()!=null){
            wrapper.apply("mo.id = mr.moduleId and ur.roleId=mr.roleId and ur.userId="+sc.getTypeName());
            List<Module> list1 = moduleMapper.modulelist(wrapper);
            List<ModulePath> mdtoListpath=new ArrayList<ModulePath>();//所有的已转的模块业务对象
            if(list1.size()>0){
            for(Module ms:list1) {
                mdtoListpath.add(CommonUtil.getModule(ms));
            }
                return new ResultData(true,20000,"查询成功",mdtoListpath,null);
          }
        }else{
        List<Module> list = moduleMapper.selectList(wrapper);
        for(Module ms:list) {
            mdtoList.add(CommonUtil.getModuleDtoByModule(ms));
        }
        }
        UITreeFactory.loadTree(mdtoList);
        return new ResultData(true,20000,"查询成功",UITreeFactory.moduleTreeList,null);
    }
    /**
     *根据角色名字精确查询角色
     * */
    public Module getOne(@RequestBody Module module){
        QueryWrapper<Module> wrapper = new QueryWrapper<>();
        wrapper.eq( "moduleName", module.getModuleName());
        return moduleMapper.selectOne(wrapper);
    }

    /**
     *
     * */
    @RequestMapping("/add")
    public Object add(@RequestBody Module module){
        Module m = this.getOne(module);
        if (m == null) {
            int r=moduleMapper.insert(module);
             long id = module.getId();
            if(r==1)
                return new ResultData(true,20000,"添加模块成功",true,id);
            else
                return new ResultData(false,20000,"添加模块失败",false,id);
        } else {
            return new ResultData(false,20001,"模块名已存在",false,null);
        }

    }
    /**
     *
     * */
    @RequestMapping("/set")
    public Object set(@RequestBody Module module){
        QueryWrapper<Module> wrapper = new QueryWrapper<>();
        wrapper.eq( "moduleName", module.getModuleName());
        wrapper.ne("id",module.getId());
        Module m = moduleMapper.selectOne(wrapper);
        if (m == null) {
            int r = moduleMapper.updateById(module);
            if(r==1)
                return new ResultData(true,20000,"修改模块成功",true,null);
            else
                return new ResultData(false,20000,"修改模块失败",false,null);
        } else {
            return new ResultData(false,20001,"修改过的模块名已存在",null,null);
        }

    }
    /**
     *
     * */
    @RequestMapping("/del")
    public Object del(@RequestBody Module module){
        int rs = moduleMapper.deleteById(module.getId());
        if(rs==1)
            return new ResultData(true,20000,"删除成功",true,null);
        else
            return new ResultData(false,20001,"删除失败",false,null);
    }
}
